import {ref, toRefs} from "vue";
import dictTypeApi from "@/api/system/type";
import {useDictStoreHook} from "@/store/dict";

let dict = {

    convertSysDictDataVoList: (sysDictDataVoList) => {
        return sysDictDataVoList.map(sysDictDataVo => {
            return {
                label: sysDictDataVo.key,
                value: sysDictDataVo.value,
                display: sysDictDataVo.display,
                defaulted: sysDictDataVo.defaulted,
            }
        });
    },

    useDictList: (...codeList) => {
        const res = ref({});
        return (() => {
            const dictStore = useDictStoreHook();
            codeList.forEach((code, index) => {
                res.value[code] = [];
                const sysDictTypeVo = dictStore.getDictTypeByCode(code);
                const sysDictDataVoList = sysDictTypeVo.sysDictDataVoList;
                if (sysDictDataVoList) {
                    res.value[code] = dict.convertSysDictDataVoList(sysDictDataVoList);
                } else {
                    dictTypeApi.infoByCode.get(code, true).then(response => {
                        const sysDictTypeVo = response.data;
                        if (sysDictTypeVo && sysDictTypeVo.sysDictDataVoList) {
                            res.value[code] = dict.convertSysDictDataVoList(sysDictTypeVo.sysDictDataVoList);
                            dictStore.setDictType(sysDictTypeVo);
                        }
                    });
                }
            })
            return toRefs(res.value);
        })()
    }

}

export default dict;